package com.hr.approval.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hr.approval.entity.ApprovalData;
import com.hr.approval.entity.param.ApprovalParam;
import com.hr.approval.entity.vo.ApprovalDataDisplay;
import com.hr.bigData.entity.ApprovalDataEchartsVO;
import com.hr.approval.entity.vo.ApprovalDataVO;
import com.hr.approval.mapper.ApprovalDataMapper;
import com.hr.approval.service.ApprovalDataService;
import com.hr.system.entity.SysUserRole;
import com.hr.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hr
 * @since 2024-05-07
 */
@Service
public class ApprovalDataServiceImpl extends ServiceImpl<ApprovalDataMapper, ApprovalData> implements ApprovalDataService {

    @Autowired
    private ApprovalDataMapper approvalDataMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    public Integer add(ApprovalData approvalData){

        return 1;
    }

    @Override
    public Integer update(ApprovalData approvalData) {
        return approvalDataMapper.updateById(approvalData);
    }

    @Override
    public void delete(Integer id) {
        approvalDataMapper.deleteById(id);
    }

    @Override
    public ApprovalData findApprovalDataById(Integer id) {
        return approvalDataMapper.selectById(id);
    }

    //分页
    @Override
    public List<ApprovalData> getApprovalDataList() {
        List<ApprovalData> approvalData = approvalDataMapper.selectList(null);
        return approvalData;
    }


    /**
     * 根据发起人查询
     * @param
     * @return
     */
//    @Override
//    public PageBean getApprovalDataListByReportId(ApprovalPWhere approvalPWhere) {
////        List<ApprovalData> approvalData = approvalDataMapper.selectList(new LambdaQueryWrapper<ApprovalData>().eq(ApprovalData::getReporterId,id));
//        approvalPWhere.setPageNum((approvalPWhere.getPageNum()-1)*approvalPWhere.getPageSize());
//
//        List<ApprovalData> approvalData = approvalDataMapper.queryData(approvalPWhere);
//
//        Long counts = approvalDataMapper.queryCounts(approvalPWhere);
//        PageBean pageBean = new PageBean(counts,approvalData);
//        return pageBean;
//    }

    @Override
    public List<ApprovalData> getApprovalDataListByReportId(Integer id) {
        List<ApprovalData> approvalData = approvalDataMapper.selectList(new LambdaQueryWrapper<ApprovalData>().eq(ApprovalData::getReporterId,id));
        return approvalData;
    }

    /**
     * 根据审批人查询
     * @param id
     * @return
     */
    @Override
    public List<ApprovalData> getApprovalDataListByApproval(Integer id) {
        List<ApprovalData> approvalData = approvalDataMapper.selectList(new LambdaQueryWrapper<ApprovalData>().eq(ApprovalData::getApprovalLeaderId,id));
        return approvalData;
    }

    /**
     * 未审批和已审批
     * @param approvalParam
     * @return
     */
    @Override
    public List<ApprovalDataVO> getApproveDataByApproval(ApprovalParam approvalParam) {
        if (approvalParam.getRoleId()!=null) {
            approvalParam.setApprovalFlag(0);  //未审批
            switch (approvalParam.getRoleId()) {
                case 5:
                    approvalParam.setState(0);
                    break;
                case 4:
                    approvalParam.setState(1);
                    break;
                case 2:
                    approvalParam.setState(2);
                    break;
                default:
                    break;
            }
        }else{
            approvalParam.setApprovalFlag(1);//已审批
            SysUserRole sysUserRole = sysUserRoleMapper.selectOne(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId,approvalParam.getUserId()));
            Integer roleId = sysUserRole.getRoleId();
            switch (roleId) {
                case 5:
                    approvalParam.setState(0);
                    break;
                case 4:
                    approvalParam.setState(1);
                    break;
                case 2:
                    approvalParam.setState(2);
                    break;
                default:
                    break;
            }
        }
        List<ApprovalDataVO> unApprove = approvalDataMapper.getApproveDataByApproval(approvalParam);
        return unApprove;
    }

    @Override
    public Integer agreeApprove(Integer id,Integer state) {
        Integer i =approvalDataMapper.agreeApprove(id,state);
        return i;
    }

    @Override
    public Integer disagreeApprove(Integer id) {
        Integer i =approvalDataMapper.disagreeApprove(id);
        return i;
    }

    @Override
    public ApprovalDataDisplay getApprovalDataById(Integer id) {
//        ApprovalData approvalData = approvalDataMapper.selectById(id);
        ApprovalDataDisplay approvalDataDisplay = approvalDataMapper.selectDataById(id);
        return approvalDataDisplay;
    }

    @Override
    public ApprovalDataEchartsVO getApprovalDataEcharts() {

        LocalDateTime todayTime =  LocalDateTime.of(LocalDateTime.now().toLocalDate(), LocalTime.MIN);
        List<ApprovalData> approvalDatas = approvalDataMapper.selectList(null
        );
        LocalDate today =  LocalDate.now();
        ApprovalDataEchartsVO approvalDataEchartsVO = new ApprovalDataEchartsVO();
        List<LocalDate> dateList = new ArrayList();
        List buList = new ArrayList();
        List shiList = new ArrayList();
        List yearList = new ArrayList();
        for (int i = 6; i >= 0; i--) {
            int a = i+1;
            dateList.add(today.minusDays(i));
            buList.add(approvalDatas.stream().filter( appr ->appr.getType()==1 && appr.getCreateTime().isAfter(todayTime.minusDays(a))&& appr.getCreateTime().isBefore(todayTime.minusDays(a-1))).count() );
            shiList.add(approvalDatas.stream().filter( appr ->appr.getType()==2 &&appr.getCreateTime().isAfter(todayTime.minusDays(a))&& appr.getCreateTime().isBefore(todayTime.minusDays(a-1))).count() );
            yearList.add(approvalDatas.stream().filter( appr ->appr.getType()==3 &&appr.getCreateTime().isAfter(todayTime.minusDays(a))&& appr.getCreateTime().isBefore(todayTime.minusDays(a-1))).count() );
        }
        approvalDataEchartsVO.setDateList(dateList);
        approvalDataEchartsVO.setBuList(buList);
        approvalDataEchartsVO.setShiList(shiList);
        approvalDataEchartsVO.setYearList(yearList);
        return approvalDataEchartsVO;
    }

    @Override
    public List<ApprovalData> getApproveDataByUserId(ApprovalParam approvalParam) {
        return approvalDataMapper.selectList(Wrappers.<ApprovalData>lambdaQuery()
                .eq(ApprovalData::getReporterId,approvalParam.getUserId())
                .eq(approvalParam.getType()!=null,ApprovalData::getType,approvalParam.getType())
        );
    }

    @Override
    public List<ApprovalData> getMyApprovalList(ApprovalData approvalData) {
        List<ApprovalData> approvalDatas = approvalDataMapper.selectList(Wrappers.<ApprovalData>lambdaQuery()
                .eq(approvalData.getReporterId()!=null ,ApprovalData::getReporterId,approvalData.getReporterId()));
        return approvalDatas;
    }
}
